package com.tpv.app.volley.demo.util;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

public class PermissionRequestActivity  extends Activity
{
    
    private static IPermission iPermissionListener;
    private String[] permissions;
    private static final String PERMISSION_KEY="permission_key";
    private static final String REQUEST_CODE="request_code";
    private int requestCode;
    public static final int ASK_REQUEST_CODE=10001;
    public static final int ASK_UM_CODE=10002;
    public static final String []  CAMERA_PERMISSONS=new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.CAMERA};
    public static final String []  VIDEO_PERMISSONS=new String[]{
            Manifest.permission.RECORD_AUDIO
            ,Manifest.permission.READ_EXTERNAL_STORAGE
            ,Manifest.permission.WRITE_EXTERNAL_STORAGE
            ,Manifest.permission.CAMERA
            ,Manifest.permission.MODIFY_AUDIO_SETTINGS
    };
    public static final String []  BASE_PERMISSONS=new String[]{Manifest.permission.READ_PHONE_STATE,Manifest.permission.ACCESS_NETWORK_STATE,Manifest.permission.ACCESS_WIFI_STATE,Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE};
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(new LinearLayout(this));
        Log.e("PermissionRequest","onCreate");
        final Bundle bundle = getIntent().getExtras();
        if (null!=bundle)
        {
            permissions = bundle.getStringArray(PERMISSION_KEY);
            requestCode = bundle.getInt(REQUEST_CODE,0);
        }
        if (null==permissions||permissions.length<=0)
        {
            finish();
            return;
        }
        Log.e("PermissionRequest","申请权限");
        requestPermission(permissions);
    }
    
    /**
     * 跳转到Activity申请权限
     *
     * @param context     Context
     * @param permissions Permission List
     * @param iPermission Interface
     */
    public static void permissionRequest(Context context,String[] permissions,int requestCode,@NonNull IPermission iPermission)
    {
        Log.e("permissionRequest","permissionRequest");
        if (PermissionUtils.hasSelfPermissions(context,permissions))
        {
            iPermission.permissionGranted(requestCode);
            return;
        }
        iPermissionListener=iPermission;
        Intent intent=new Intent(context,PermissionRequestActivity.class);
        Bundle bundle=new Bundle();
        bundle.putStringArray(PERMISSION_KEY,permissions);
        bundle.putInt(REQUEST_CODE,requestCode);
        intent.putExtras(bundle);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP);
        context.startActivity(intent);
        if (context instanceof Activity)
        {
            ((Activity) context).overridePendingTransition(0,0);
        }
    }
    
    /**
     * 申请权限
     *
     * @param permissions permission list
     */
    private void requestPermission(String[] permissions)
    {
        if (PermissionUtils.hasSelfPermissions(this,permissions))
        {
            if (null!=iPermissionListener)
            {
                iPermissionListener.permissionGranted(requestCode);
            }
            Log.e("PermissionRequest","获取权限");
            finish();
            overridePendingTransition(0,0);
        }else {
            ActivityCompat.requestPermissions(this,permissions,requestCode);
            Log.e("PermissionRequest","申请权限");
        }
    }
    
    @Override
    public void onRequestPermissionsResult(int requestCode, @Nullable String[] permissions,@NonNull int[] grantResults)
    {
        if (PermissionUtils.verifyPermissions(grantResults))
        {
            if (null!=iPermissionListener)
            {
                iPermissionListener.permissionGranted(requestCode);
            }
        }else{
            if (!PermissionUtils.shouldShowRequestPermissionsRationale(this,permissions))
            {
                //权限被拒绝并且选中不再提示
                if (permissions.length!=grantResults.length)return;
                List<String> denyList=new ArrayList<>();
                for (int i=0;i<grantResults.length;i++)
                {
                    if (grantResults[i]==-1)
                    {
                        denyList.add(permissions[i]);
                    }
                }
                if (null!=iPermissionListener)
                {
                    iPermissionListener.permissionDeny(requestCode,denyList);
                }
            }else {
                if (null!=iPermissionListener)
                {
                    iPermissionListener.permissionCancel(requestCode);
                }
            }
        }
        finish();
        overridePendingTransition(0,0);
    }
    
    public static void showPermissionGrantCancel()
    {
    }
    public static void showPermissionGrantDenny(Context context)
    {
    }
}
